package team2.arsystem.GUI;

import java.util.ArrayList;
import team2.arsystem.Controller.ApplicationController;
import team2.arsystem.Utilities.CommandConstants;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AddToDebitScreen2 extends Activity{

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		ScrollView sv = new ScrollView(this);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		sv.addView(ll);

		TextView addToDebitTextView = new TextView(this);
		addToDebitTextView.setText("\n Add to my debit account \n");
		ll.addView(addToDebitTextView);
		
		TextView membersTextView = new TextView(this);
		membersTextView.setText("\n Members in this transaction: \n");
		ll.addView(membersTextView);

		/*Get the members that the user has already added in the application*/
		final ArrayList<String> members= ApplicationController.getApplicationController().passCommandAndFetchResult(CommandConstants.getMembers);
		final ArrayList<CheckBox> cb=new ArrayList<CheckBox>();

		 for(int i = 0; i < members.size(); i++) {
			
			cb.add(new CheckBox(this));
			cb.get(i).setText(members.get(i));
			ll.addView(cb.get(i));
		}
		
		LinearLayout innerll = new LinearLayout(this);
		innerll.setOrientation(LinearLayout.HORIZONTAL);
		
		
		Button okButton = new Button(this);
		okButton.setText("Add this debit transaction");
		innerll.addView(okButton);
		
		Button cancelButton = new Button(this);
		cancelButton.setText("Cancel");
		innerll.addView(cancelButton);
		
		ll.addView(innerll);
		this.setContentView(sv);
		
		okButton.setOnClickListener(new OnClickListener() {
			
           public void onClick(View v) {
        	
           String selectedMembers = "";
        		
           for(int i = 0; i < members.size(); i++) {
        				
        			if(cb.get(i).isChecked())
        					selectedMembers=selectedMembers+","+cb.get(i).getText().toString()+",";
        			
        			selectedMembers = selectedMembers.substring(0,selectedMembers.length());
        			
        	}
        	
           
           
           String debitTxDescription = getIntent().getExtras().getString("team2.arsystem.GUI.AddTODebitScreen.txDesc");
       	   double debitTxAmount =getIntent().getExtras().getDouble("team2.arsystem.GUI.AddTODebitScreen.amount");
       	   boolean divideAmongMembers = getIntent().getExtras().getBoolean("team2.arsystem.GUI.AddTODebitScreen.divideAmongMembers");
       	  
        
          
           //Object[] addDebitTxParams = {AddToDebitAccount.debitTxDescription,AddToDebitAccount.debitTxAmount,AddToDebitAccount.divideAmongMembers,AddToDebitAccount.transactionDate,AddToDebitAccount.reminderDate,selectedMembers};
       	Object[] addDebitTxParams = {debitTxDescription,debitTxAmount,divideAmongMembers,AddToDebitAccount.transactionDate,AddToDebitAccount.reminderDate,selectedMembers};
        	if((ApplicationController.getApplicationController().passCommand(CommandConstants.addDebitTx,addDebitTxParams)))
        	{
        		try
        		{
        			Dialog dialogOK = new Dialog(AddToDebitScreen2.this);
        			dialogOK.setContentView(R.layout.popupbox);
        			dialogOK.setTitle("Account Reminder System");
        			
        		        			
        			TextView dialogText = (TextView)dialogOK.findViewById(R.id.textViewDialogMessage);
        			dialogText.setText("The debit transaction was successfully added");
        			
        			
                    Button button = (Button) dialogOK.findViewById(R.id.buttonDialogOK);
                   
                    button.setOnClickListener(new OnClickListener() {
                   
                        public void onClick(View v) {
                        	
                        	try
                        	{
                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"DebitMenuScreen");
                    		 Intent myIntent = new Intent(AddToDebitScreen2.this,menuScreen);
                    		 startActivity(myIntent);
                        	}
                        	catch(Exception e){
                    			 e.printStackTrace();
                    		 }
                    		 
                        }
                    });
        		    
            		 
                    dialogOK.show();
        		}
        		 catch(Exception e)
        		 {
        			 e.printStackTrace();
        		 }
        	}
        		
			}
		});
		
           cancelButton.setOnClickListener(new OnClickListener() {
			
        	public void onClick(View v) {
        		
        	  	try
                 	{
                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"DebitMenuScreen");
                    		 Intent myIntent = new Intent(AddToDebitScreen2.this,menuScreen);
                    		 startActivity(myIntent);
                        	}
                        	catch(Exception e)
                        	{
                    			 e.printStackTrace();
                    		 }
                    		 
                        }
        	
                    });
        		    
	}
	
	

}
